package com.ss.android.common.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Process;
import android.provider.Settings;
import android.text.TextUtils;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.reflect.Reflect;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public final class ToolUtils {
    private static String a = ":push";
    private static boolean b = false;
    private static boolean c = false;
    private static String d = null;

    public static String getEmuiInfo() {
        return getSystemProperty("ro.build.version.emui");
    }

    public static boolean isInstalledApp(Context context, Intent intent) {
        List v0 = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return v0 != null && v0.size() > 0;
    }

    public static Intent getLaunchIntentForPackage(Context arg3, String arg4) {
        Intent v0 = arg3.getPackageManager().getLaunchIntentForPackage(arg4);
        if (v0 != null) {
            if (!v0.hasCategory("android.intent.category.LAUNCHER")) {
                v0.addCategory("android.intent.category.LAUNCHER");
                Logger.d("ToolUtils", "add category LAUNCHER in launch intent");
            }

            v0.setPackage(null);
            v0.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            v0.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        return v0;
    }

    public static void a(String arg0) {
        a = arg0;
    }

    public static long a(File arg8, boolean arg9) {
        long v0 = 0;
        try {
            if ((arg8.exists()) || !arg8.isDirectory()) {
                File[] v3 = arg8.listFiles();
                int v4 = v3.length;
                int v2_1;
                for (v2_1 = 0; v2_1 < v4; ++v2_1) {
                    File v5 = v3[v2_1];
                    if (v5.isDirectory()) {
                        v0 += a(v5, arg9);
                    } else if (v5.isFile()) {
                        v0 += v5.length();
                    }
                }
            }

            return v0;
        } catch (Throwable v2) {
            return v0;
        }
    }

    public static int getLocationMode(Context arg4) {
        int v2 = -1;
        if (arg4 == null) {
            int v0 = v2;
            return v0;
        }

        int v1 = 0;
        try {
            if (!android.support.design.a.isTextEmpty(Settings.Secure.getString(arg4.getContentResolver(), "location_providers_allowed"))) {
                Object v0_2 = arg4.getSystemService(Context.LOCATION_SERVICE);
                if (((LocationManager) v0_2).isProviderEnabled("gps")) {
                    v1 = 1;
                }

                if (((LocationManager) v0_2).isProviderEnabled("network")) {
                    v1 |= 2;
                }

                if (!((LocationManager) v0_2).isProviderEnabled("passive")) {
                    return v1;
                }

                return v1 | 4;
            }
            return v1;
        } catch (Throwable v0_1) {
            return v2;
        }
    }

    public static void runApplication(Context arg3, String arg4, String arg5) {
        boolean v0 = false;
        if (!android.support.design.a.isTextEmpty(arg4)) {
            v0 = isAppInstalled(arg3, arg4);
        }

        if (v0) {
            Intent v0_1 = getLaunchIntentForPackage(arg3, arg4);
            if (v0_1 != null) {
                arg3.startActivity(v0_1);
                return;
            }
        }

        if (!android.support.design.a.isTextEmpty(arg5)) {
            try {
                arg3.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(arg5)));
                return;
            } catch (Exception v0_2) {
            }
        }

        if (!android.support.design.a.isTextEmpty(arg4)) {
            Intent v1 = new Intent("android.intent.action.VIEW", Uri.parse("market://details?id=" + arg4));
            v1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                arg3.startActivity(v1);
            } catch (Exception v0_2) {
            }
        }
    }

    private static void a(String arg4, Set arg5) throws Exception {
        File v0 = new File(arg4);
        if ((v0.exists()) && (v0.isDirectory())) {
            File[] v1 = v0.listFiles();
            int v2 = v1.length;
            int v0_1;
            for (v0_1 = 0; v0_1 < v2; ++v0_1) {
                if (v1[v0_1].isDirectory()) {
                    a(v1[v0_1].getAbsolutePath(), arg5);
                } else {
                    String v3 = v1[v0_1].getName();
                    if (arg5 != null && (arg5.contains(v3))) {
                        continue;
                    } else {
                        v1[v0_1].delete();
                    }
                }
            }
        }
    }

    public static boolean isMiui() {
        if (!c) {
            try {
                if (Class.forName("miui.os.Build") == null) {
                } else {
                    b = true;
                    c = true;
                    boolean v0_1 = b;
                    return v0_1;
                }
            } catch (Exception v0) {
            }
            c = true;
        }

        return b;
    }

    public static boolean b(String arg3) {
        boolean v0 = true;
        if (TextUtils.isEmpty(((CharSequence) arg3))) {
            arg3 = getSystemProperty("ro.build.version.emui");
        }

        if (((TextUtils.isEmpty(((CharSequence) arg3))) || !arg3.toLowerCase().startsWith("emotionui")) && !d()) {
            v0 = false;
        }

        return v0;
    }

    @SuppressLint("WrongConstant")
    public static boolean isAppInstalled(Context context, String packageName) {
        if (context != null && !android.support.design.a.isTextEmpty(packageName)) {
            PackageManager packageManager = context.getPackageManager();
            try {
                return packageManager.getPackageInfo(packageName, 16777216) != null;
            } catch (Exception exception) {
                return false;
            }
        }
        return false;
    }

    public static boolean isMessageProcess(Context arg2) {
        String v0 = getCurProcessName(arg2);
        boolean v0_1 = v0 == null || !v0.endsWith(a) ? false : true;
        return v0_1;
    }

    public static boolean b(Context arg5, String arg6, String arg7) {
        boolean v0 = true;
        boolean v1 = false;
        if (arg5 != null && !android.support.design.a.isTextEmpty(arg6) && !android.support.design.a.isTextEmpty(arg7)) {
            try {
                File v2 = new File(arg6);
                if (!v2.exists()) {
                    return false;
                }

                PackageInfo v2_1 = arg5.getPackageManager().getPackageArchiveInfo(v2.getAbsolutePath(), 1);
                if (v2_1 == null) {
                    return v1;
                }

                if (!v2_1.packageName.equals(arg7)) {
                    return v1;
                }

                int v3 = v2_1.versionCode;
                try {
                    v2_1 = arg5.getPackageManager().getPackageInfo(arg7, 1);
                } catch (PackageManager.NameNotFoundException v2_2) {
                    v2_1 = null;
                }

                if (v2_1 == null) {
                    return v1;
                }

                if (v3 != v2_1.versionCode) {
                    return false;
                }
                return v0;
            } catch (Exception v0_1) {
                label_33:
                v0_1.printStackTrace();
                return v1;
            }
        }
        return v1;
    }

    public static boolean isMainProcess(Context context) {
        String processName = getCurProcessName(context);
        if ((processName == null || !processName.contains(":")) && (processName != null && (processName.equals(context.getPackageName())))) {
            return true;
        }
        return false;
    }

    public static void c(String arg4) throws Exception {
        File v0 = new File(arg4);
        if (v0.exists()) {
            File[] v1 = v0.listFiles();
            int v2 = v1.length;
            int v0_1;
            for (v0_1 = 0; v0_1 < v2; ++v0_1) {
                if (v1[v0_1].isDirectory()) {
                    g(v1[v0_1].getAbsolutePath());
                } else if (v1[v0_1].isFile()) {
                    v1[v0_1].delete();
                }
            }
        }
    }

    public static void sendMediaScannerScanFileBroadcast(Context context, String dir) {
        try {
            context.sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.fromFile(new File(dir))));
        } catch (Exception v0) {
            Logger.w("ToolUtils", "add image media exception: " + v0);
        }
    }

    public static boolean c() {
        boolean v0;
        if ((android.support.design.a.isTextEmpty(Build.DISPLAY)) || Build.DISPLAY.indexOf("Flyme") < 0) {
            if (!android.support.design.a.isTextEmpty(Build.USER) && (Build.USER.equals("flyme"))) {
                v0 = true;
                return v0;
            }

            v0 = false;
        } else {
            v0 = true;
            return v0;
        }

        return v0;
    }

    public static String getCurProcessName(Context arg4) {
        Object v0_2;
        String v0 = d;
        if (android.support.design.a.isTextEmpty(v0)) {
            try {
                int mypid = Process.myPid();
                Iterator v2 = ((ActivityManager) arg4.getSystemService(Context.ACTIVITY_SERVICE))
                        .getRunningAppProcesses().iterator();
                do {
                    if (!v2.hasNext()) {
                        String processName = e();
                        d = processName;
                        return processName;
                    }
                    v0_2 = v2.next();
                } while (((ActivityManager.RunningAppProcessInfo) v0_2).pid != mypid);
                if (Logger.debug()) {
                    Logger.d("Process", "processName = " + ((ActivityManager.RunningAppProcessInfo) v0_2).processName);
                }

                v0 = ((ActivityManager.RunningAppProcessInfo) v0_2).processName;
                d = v0;
                return v0;
            } catch (Exception v0_1) {
                v0_1.printStackTrace();
            }
            v0 = e();
            d = v0;
        }
        return v0;
    }

    public static void d(String arg5) throws Exception {
        File v0 = new File(arg5);
        if (v0.exists()) {
            File[] v1 = v0.listFiles();
            int v2 = v1.length;
            int v0_1;
            for (v0_1 = 0; v0_1 < v2; ++v0_1) {
                if (v1[v0_1].isDirectory()) {
                    a(v1[v0_1].getAbsolutePath(), null);
                } else if (v1[v0_1].isFile()) {
                    v1[v0_1].getName();
                    v1[v0_1].delete();
                }
            }
        }
    }

    public static boolean d(Context arg6, String arg7) {
        boolean v0 = true;
        boolean v1 = false;
        if (arg6 != null && arg7 != null && !android.support.design.a.isTextEmpty(arg7)) {
            try {
                File v2 = new File(arg7);
                if (!v2.exists()) {
                    return false;
                }

                PackageInfo v2_1 = arg6.getPackageManager().getPackageArchiveInfo(v2.getAbsolutePath(), 1);
                if (v2_1 == null) {
                    return v1;
                }

                String v3 = v2_1.packageName;
                int v4 = v2_1.versionCode;
                try {
                    v2_1 = arg6.getPackageManager().getPackageInfo(v3, 1);
                } catch (PackageManager.NameNotFoundException v2_2) {
                    v2_1 = null;
                }

                if (v2_1 == null) {
                    return v1;
                }

                if (v4 > v2_1.versionCode) {
                    return false;
                }
                return v0;
            } catch (Exception v0_1) {
                v0_1.printStackTrace();
                return v1;
            }
        }

        return v1;
    }

    private static boolean d() {
        boolean v0 = false;
        try {
            if ((android.support.design.a.isTextEmpty(Build.BRAND)) || !Build.BRAND.toLowerCase().startsWith("huawei")) {
                if (android.support.design.a.isTextEmpty(Build.MANUFACTURER)) {
                    return v0;
                } else if (!Build.MANUFACTURER.toLowerCase().startsWith("huawei")) {
                    return v0;
                }
            }
        } catch (Throwable v1) {
            return v0;
        }

        return true;
    }

    public static String e(String arg4) {
        String v0_1;
        if (!android.support.design.a.isTextEmpty(arg4)) {
            try {
                if (Logger.debug()) {
                    Logger.d("Process", "before handle = " + arg4);
                }

                v0_1 = arg4.replaceAll(".." + File.separator, "");
                if (Logger.debug()) {
                    Logger.d("Process", "after handle = " + v0_1);
                }
                arg4 = v0_1;
            } catch (Exception v1) {
            }
        }

        return arg4;
    }

    public static void e(Context arg3, String arg4) {
        if (arg3 != null && !android.support.design.a.isTextEmpty(arg4)) {
            try {
                Intent v1 = new Intent("android.intent.action.DIAL", Uri.parse("tel:" + Uri.encode(arg4)));
                if (!(arg3 instanceof Activity)) {
                    v1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }

                arg3.startActivity(v1);
            } catch (Exception v0) {
            }
        }
    }

    public static String e(Context arg5) {
        Object v0_3;
        if (arg5 == null) {
            String v0 = "";
            return v0;
        }

        try {
            List v0_2 = ((ActivityManager) arg5.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(5);
            if (v0_2 == null) {
                return "";
            }

            String v1 = arg5.getPackageName();
            StringBuilder v2 = new StringBuilder();
            Iterator v3 = v0_2.iterator();
            do {
                if (!v3.hasNext()) {
                    return "";
                }

                v0_3 = v3.next();
                if (v0_3 == null) {
                    continue;
                }

                if (((ActivityManager.RunningTaskInfo) v0_3).baseActivity == null) {
                    continue;
                }
            }
            while (!v1.equals(((ActivityManager.RunningTaskInfo) v0_3).baseActivity.getPackageName()));

            v2.append("id = ").append(((ActivityManager.RunningTaskInfo) v0_3).id).append(" ");
            v2.append("description = ").append(((ActivityManager.RunningTaskInfo) v0_3).description).append(" ");
            v2.append("number_of_activities = ").append(((ActivityManager.RunningTaskInfo) v0_3).numActivities).append(" ");
            v2.append("number_of_running_activities = ").append(((ActivityManager.RunningTaskInfo) v0_3).numRunning).append(" ");
            v2.append("topActivity = ").append(((ActivityManager.RunningTaskInfo) v0_3).topActivity.toString()).append(" ");
            v2.append("baseActivity = ").append(((ActivityManager.RunningTaskInfo) v0_3).baseActivity.toString());
            return v2.toString();
        } catch (Throwable v0_1) {
        }

        return "";
    }

    private static String e() {
        BufferedReader v1_1 = null;
        String v0 = null;
        try {
            v1_1 = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/" + Process.myPid() + "/cmdline"), "iso-8859-1"));
            StringBuilder sb = new StringBuilder();
            while (true) {
                int v3 = v1_1.read();
                if (v3 <= 0) {
                    break;
                }

                sb.append(((char) v3));
            }
            if (Logger.debug()) {
                Logger.d("Process", "get processName = " + sb.toString());
            }
            return sb.toString();
        } catch (Throwable v0_1) {
            v0_1.printStackTrace();
            return null;
        } finally {
            if (v1_1 != null) {
                try {
                    v1_1.close();
                } catch (Exception e) {

                }
            }
        }
    }

    public static String getInternalCacheDir(Context context) throws NullPointerException {
        String v0 = null;
        if (context == null) {
            throw new NullPointerException("Context is NUll");
        }

        try {
            if (context.getCacheDir() != null) {
                v0 = context.getCacheDir().getPath();
            } else {
                File v1_1 = context.getDir("/data/data/" + context.getPackageName() + "/cache/", 0);
                if (v1_1 == null) {
                } else {
                    v0 = v1_1.getPath();
                }
            }
        } catch (Throwable v1) {
        }

        if (android.support.design.a.isTextEmpty(v0)) {
            throw new NullPointerException("Cannot Create Cache Dir");
        }
        return v0;
    }

    public static String getProviderAuthority(Context context, String providerClassName) {
        int v1_1;
        String v0 = null;
        if (context != null && !android.support.design.a.isTextEmpty(providerClassName)) {
            try {
                ProviderInfo[] providerInfos = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PROVIDERS).providers;
                int length = providerInfos.length;
                v1_1 = 0;
                while (true) {
                    if (v1_1 >= length) {
                        return v0;
                    }

                    ProviderInfo providerInfo = providerInfos[v1_1];
                    if (!providerClassName.equals(providerInfo.name)) {
                        ++v1_1;
                        continue;
                    }
                    return providerInfo.authority;
                }
            } catch (Exception v1) {
                return v0;
            }


        }

        return v0;
    }

    private static String getSystemProperty(String arg7) {
        Throwable v1_1;
        String v0_1;
        Throwable v6;
        BufferedReader v2 = null;
        String v1 = null;
        try {
            v2 = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("getprop " + arg7).getInputStream()), 1024);
            v0_1 = v2.readLine();
            return v0_1;
        } catch (Throwable v0) {
            Logger.e("ToolUtils", "Unable to read sysprop " + arg7, v0);
            return v1;
        } finally {
            if (v2 != null) {
                try {
                    v2.close();
                } catch (IOException v1_2) {
                    Logger.e("ToolUtils", "Exception while closing InputStream", ((Throwable) v1_2));
                }
            }
        }
    }

    private static void g(String arg5) throws Exception {
        File v1 = new File(arg5);
        if ((v1.exists()) && (v1.isDirectory())) {
            File[] v2 = v1.listFiles();
            int v3 = v2.length;
            int v0;
            for (v0 = 0; v0 < v3; ++v0) {
                if (v2[v0].isDirectory()) {
                    g(v2[v0].getAbsolutePath());
                } else {
                    v2[v0].delete();
                }
            }

            v1.delete();
        }
    }

    public static int g(Context arg11) {
        int v1 = -1;
        if (arg11 == null) {
            int v0 = v1;
            return v0;
        }

        try {
            Object v0_2 = arg11.getSystemService(Context.APP_OPS_SERVICE);
            ApplicationInfo v4 = arg11.getApplicationInfo();
            String v5 = arg11.getApplicationContext().getPackageName();
            int v4_1 = v4.uid;
            Reflect v6 = Reflect.on(v0_2);
            if (((Integer) v6.call("checkOpNoThrow",
                    new Class[]{Integer.TYPE, Integer.TYPE, String.class},
                    new Object[]{Integer.valueOf(((Integer) v6.get("OP_POST_NOTIFICATION", new Class[]{Integer.TYPE}).target).intValue()),
                            Integer.valueOf(v4_1), v5}).target).intValue() != 0) {
                return 0;
            }
        } catch (Throwable v0_1) {
            v0_1.printStackTrace();
            return v1;
        }

        return 1;
    }
}

